package edu.uwm.cs552;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.DefaultListCellRenderer;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;


/**
 * Render a terrain with a little icon showing the visual rendering of 
 * a terrain.
 */
public class TerrainRenderer extends DefaultListCellRenderer {
	/**
	 * Keep Eclipse Happy
	 */
	private static final long serialVersionUID = 1L;

	private ImageIcon[] icons;
	
	private static final int ICON_SIZE = 20;

	public Component getListCellRendererComponent(JList list,
			Object value,
			int index,
			boolean isSelected,
			boolean hasFocus) {
		if (icons == null) {
			icons = new ImageIcon[Terrain.values().length+1];
			for (int i=0; i < icons.length; ++i) {
				BufferedImage im = new BufferedImage(ICON_SIZE,ICON_SIZE, BufferedImage.TYPE_INT_ARGB);
				Graphics g = im.getGraphics();
				g.translate(ICON_SIZE/2, ICON_SIZE/2);
				if (i < Terrain.values().length) {
					Terrain.values()[i].draw(g, HexCoordinate.ORIGIN, ICON_SIZE);
				}
				icons[i] = new ImageIcon(im);
			}
		}
		JLabel label =
			(JLabel)super.getListCellRendererComponent(list,
					value == null ? "nothing" : value,
					index,
					isSelected,
					hasFocus);
		if (value instanceof Terrain) {
			int i = ((Terrain)value).ordinal();
			label.setIcon(icons[i]);
		} else {
			// Clear old icon; needed in 1st release of JDK 1.2
			label.setIcon(icons[Terrain.values().length]); 
		}
		return(label);
	}
}
